//
//  NCChatViewLeftAudioCell.m
//  AGEmojiKeyboard
//
//  Created by LINAICAI on 2018/4/18.
//

#import "NCChatViewLeftAudioCell.h"
#import "WBTLAudioPlayer.h"
@implementation NCChatViewLeftAudioCell
- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:NCChatViewCellAudioPlayStopNotifi object:nil];
}
- (void)awakeFromNib {
    [super awakeFromNib];
    self.usrNameLabel.font = [UIFont systemFontOfSize:13];
    self.usrNameLabel.textColor = [UIColor grayColor];
    
    
    self.avatorImage.layer.cornerRadius = 34.0/2;
    self.avatorImage.layer.masksToBounds = YES;


    self.audioTimeLabel.font = [UIFont systemFontOfSize:12];
    self.audioTimeLabel.textColor = [UIColor lightGrayColor];
    
    self.contentImage.tintColor = [UIColor colorWithRed:241/255.0 green:240/255.0 blue:240/255.0 alpha:1.0];
    self.contentImage.image = [[NCChatUIImage(@"icon_receiver_node_normal")imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] resizableImageWithCapInsets:UIEdgeInsetsMake(20, 20, 20, 20) resizingMode:UIImageResizingModeStretch];
    [self.contentImage addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(playAudio)]];
    self.contentImage.userInteractionEnabled = YES;
    
    self.audioImage.image = NCChatUIImage(@"audio_left_3");
    
    ///监听停止播放的通知
    [[NSNotificationCenter defaultCenter]addObserverForName:NCChatViewCellAudioPlayStopNotifi object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        [self.audioImage stopAnimating];
    }];
}
- (NSArray<UIImage *> *)animationImages{
    static NSArray *images = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        images = @[NCChatUIImage(@"audio_left_1"),NCChatUIImage(@"audio_left_2"),NCChatUIImage(@"audio_left_3")];
    });
    return images;
}
- (void)playAudio{
    ///播放音频
    if ([[WBTLAudioPlayer sharedAudioPlayer]isPlaying]) {
        ///如果还有其他语音在播放,停止
        [[WBTLAudioPlayer sharedAudioPlayer]stopPlayingAudio];
    }
    ///如果没有可播放的音频,不执行下面的操作
    if (!self.message.filePath && !self.message.filePath.length && !self.message.data) {
        return;
    }
    [self.audioImage setAnimationImages:[self animationImages]];
    [self.audioImage setAnimationDuration:1.0];
    [self.audioImage setAnimationRepeatCount:0];
    [self.audioImage startAnimating];
    
    ///优先使用本地文件来播放,没有再从二进制播放
    
    NSData *data = (self.message.filePath && self.message.filePath.length)?[NSData dataWithContentsOfURL:[NSURL fileURLWithPath:self.message.filePath]]:self.message.data;
    
    [[WBTLAudioPlayer sharedAudioPlayer]playAudioWith:data complete:^(BOOL finished) {
        if (finished) {
            [self.audioImage stopAnimating];
        }
    }];
}
- (void)setMessage:(NCChatAudioMessage *)message{
    [super setMessage:message];
    self.audioTimeLabel.text = [NSString stringWithFormat:@"%ld\"",message.time];
    
    //根据语音长度调整气泡的宽度
    [self.contentImage mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(message.time*5+70);
        
    }];
    
}
- (void)setupStatus{
    [super setupStatus];
    switch (self.message.status) {
        case NCChatMessageSendSuccess:
        {
            //发送成功
            NSLog(@"NCChatMessageSendSuccess");
            [self.audioTimeLabel setHidden:NO];
        }
            break;
        case NCChatMessageSending:
        {
            NSLog(@"NCChatMessageSending");
            //发送中
            [self.audioTimeLabel setHidden:YES];
        }
            break;
        case NCChatMessageSendFail:
        {
            NSLog(@"NCChatMessageSendFail");
            //发送失败
            [self.audioTimeLabel setHidden:YES];
            
        }
            break;
            
        default:
            break;
    }
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
@end
